import { Button, Checkbox, Form, Input } from "antd";
import React from "react";
import { useNavigate } from "react-router-dom";
import { useAppDispatch } from "../../app/hooks";
import { addList } from "../list/listSlice";

const App = () => {
	const dispatch = useAppDispatch()
	const navigate = useNavigate()
  const onFinish = (values: any) => {
    // console.log("Success:", values);
		if(!/1[3-9]\d{9}/.test(values.tel)) {
			alert('手机号不正确')
		}

		dispatch(addList(values)).then(() => {
			navigate('/list')
		})
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <Form
      name="basic"
      labelCol={{
        span: 4,
      }}
      wrapperCol={{
        span: 16,
      }}
      initialValues={{
        remember: true,
      }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="姓名"
        name="name"
        rules={[
          {
            required: true,
            message: "请输入你的姓名!",
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="地址"
        name="address"
        rules={[
          {
            required: true,
            message: "请输入你的地址!",
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="手机号"
        name="tel"
        rules={[
          {
            required: true,
            message: "请输入你的电话!",
          },
        ]}
      >
        <Input />
      </Form.Item>

			<Form.Item
        label="性别"
        name="sex"
        rules={[
          {
            required: true,
            message: '请输入你的性别!',
          },
        ]}
      >
        <Input />
      </Form.Item>

			<Form.Item
        label="标题"
        name="title"
        rules={[
          {
            required: true,
            message: '请输入你的标题!',
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        wrapperCol={{
          offset: 8,
          span: 16,
        }}
      >
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

export default App;
